package com.ytzl.itrip.controller;

import com.alibaba.fastjson.JSONArray;
import com.ytzl.itrip.common.DtoUtil;
import com.ytzl.itrip.common.EmptyUtils;
import com.ytzl.itrip.common.ErrorCode;
import com.ytzl.itrip.common.Page;
import com.ytzl.itrip.dto.Dto;
import com.ytzl.itrip.pojo.ItripHotelOrder;
import com.ytzl.itrip.pojo.ItripHotelRoom;
import com.ytzl.itrip.pojo.ItripUser;
import com.ytzl.itrip.vo.order.ItripPersonalHotelOrderVO;
import com.ytzl.itrip.vo.order.ItripPersonalOrderRoomInfoVO;
import com.ytzl.itrip.vo.order.ItripSearchOrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by sam on 2018/2/10.
 */
@Controller
@Api(tags = "酒店订单模块业务")
@RequestMapping("/api/hotelorder")
public class HotelOrderController extends BaseController {

    @ApiOperation(value = "查询用户订单", httpMethod = "POST", produces = "application/json",
            response = Dto.class, notes = "根据条件查询个人订单列表，并分页显示\" +\n" +
            "            \"<p>订单类型(orderType)（-1：全部订单 0:旅游订单 1:酒店订单 2：机票订单）：</p>\" +\n" +
            "            \"<p>订单状态(orderStatus)（0：待支付 1:已取消 2:支付成功 3:已消费 4：已点评）：</p>\" +\n" +
            "            \"<p>对于页面tab条件：</p>\" +\n" +
            "            \"<p>全部订单（orderStatus：-1）</p>\" +\n" +
            "            \"<p>未出行（orderStatus：2）</p>\" +\n" +
            "            \"<p>待付款（orderStatus：0）</p>\" +\n" +
            "            \"<p>待评论（orderStatus：3）</p>\" +\n" +
            "            \"<p>已取消（orderStatus：1）</p>\" +\n" +
            "            \"<p>成功：success = ‘true’ | 失败：success = ‘false’ 并返回错误码，如下：</p>\" +\n" +
            "            \"<p>错误码：</p>\" +\n" +
            "            \"<p>100501 : 请传递参数：orderType </p>\" +\n" +
            "            \"<p>100502 : 请传递参数：orderStatus </p>\" +\n" +
            "            \"<p>100503 : 获取个人订单列表错误 </p>\" +\n" +
            "            \"<p>100000 : token失效，请重登录 </p>")
    @RequestMapping(value = "/getpersonalorderlist", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Dto getpersonalorderlist(@RequestBody ItripSearchOrderVO itripSearchOrderVO, HttpServletRequest request) {
        String token = request.getHeader("token");
        //获取当前登录用户
        ItripUser itripUser = validationToken.getCurrentUser(token);
        if (itripUser != null) {
            if (itripSearchOrderVO.getOrderType() == null) {
                return DtoUtil.returnFail("请传递参数OrderType", ErrorCode.BIZ_QUERY_PARAM_ORDERTYPE);
            }
            if (itripSearchOrderVO.getOrderStatus() == null) {
                return DtoUtil.returnFail("请传递参数OrderStatus", ErrorCode.BIZ_QUERY_PARAM_ORDERSTATUS);
            }
            Map<String, Object> param = new HashMap();
            param.put("orderStatus", itripSearchOrderVO.getOrderStatus() == -1 ? null : itripSearchOrderVO.getOrderStatus());
            param.put("orderType", itripSearchOrderVO.getOrderType() == -1 ? null : itripSearchOrderVO.getOrderType());
            param.put("endDate", itripSearchOrderVO.getEndDate());
            param.put("linkUserName", itripSearchOrderVO.getLinkUserName());
            param.put("userId", itripUser.getId());
            param.put("orderNo", itripSearchOrderVO.getOrderNo());
            param.put("startDate", itripSearchOrderVO.getStartDate());
            try {
                Page<ItripHotelOrder> page = itripHotelOrderService.queryItripHotelOrderPageByMap(param, itripSearchOrderVO.getPageNo(), itripSearchOrderVO.getPageSize());
                return DtoUtil.returnSuccess("获取个人订单列表成功", page);
            } catch (Exception e) {
                e.printStackTrace();
                return DtoUtil.returnFail("查询个人订单失败", ErrorCode.BIZ_QUERY_GET_PERSONAL_ORDER_LIST_FAILED);
            }
        } else {
            return DtoUtil.returnFail("Token已失效", ErrorCode.BIZ_TOKEN_INVALID);
        }
    }

    @ApiOperation(value = "通过订单ID 查看订单详情", httpMethod = "GET", response = Dto.class, produces = "application/json", notes = "根据订单ID查看个人订单详情" +
            "<p>订单状态(orderStatus)（0：待支付 1:已取消 2:支付成功 3:已消费 4：已点评）：</p>" +
            "<p>订单流程：</p>" +
            "<p>订单状态(0：待支付 2:支付成功 3:已消费 4:已点评)的流程：{\"1\":\"订单提交\",\"2\":\"订单支付\",\"3\":\"支付成功\",\"4\":\"入住\",\"5\":\"订单点评\",\"6\":\"订单完成\"}</p>" +
            "<p>订单状态(1:已取消)的流程：{\"1\":\"订单提交\",\"2\":\"订单支付\",\"3\":\"订单取消\"}</p>" +
            "<p>支持支付类型(roomPayType)：{\"1\":\"在线付\",\"2\":\"线下付\",\"3\":\"不限\"}</p>" +
            "<p>成功：success = ‘true’ | 失败：success = ‘false’ 并返回错误码，如下：</p>" +
            "<p>错误码：</p>" +
            "<p>100525 : 请传递参数：orderId </p>" +
            "<p>100526 : 没有相关订单信息 </p>" +
            "<p>100527 : 获取个人订单信息错误 </p>" +
            "<p>100000 : token失效，请重登录 </p>")
    @RequestMapping(value = "/getpersonalorderinfo/{orderId}", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Dto getpersonalorderinfo(@PathVariable @ApiParam(value = "orderId", required = true, name = "订单编号") String orderId, HttpServletRequest request) {
        String token = request.getHeader("token");
        //通过token获取用户
        ItripUser currentUser = validationToken.getCurrentUser(token);
        if (currentUser != null) {
            //根据订单ID查询
            if (EmptyUtils.isNotEmpty(orderId)) {
                try {
                    ItripHotelOrder itripHotelOrder = itripHotelOrderService.getItripHotelOrderById(Long.valueOf(orderId));
                    if (EmptyUtils.isNotEmpty(itripHotelOrder)) {
                        ItripPersonalHotelOrderVO itripPersonalHotelOrderVO = new ItripPersonalHotelOrderVO();
                        BeanUtils.copyProperties(itripHotelOrder, itripPersonalHotelOrderVO);
                        //查询预定酒店信息，根据酒店Id
                        ItripHotelRoom hotelRoom = itripHotelRoomService.getItripHotelRoomById(itripHotelOrder.getRoomId());
                        if (EmptyUtils.isNotEmpty(hotelRoom))
                            itripPersonalHotelOrderVO.setRoomPayType(hotelRoom.getPayType());
                        //根据订单的状态给前天提供两种节点
                        //1:{"1":"订单提交","2":"订单支付","3":"订单取消"}
                        //2:{"1":"订单提交","2":"订单支付","3":"支付成功","4":"入住","5":"订单点评","6":"订单完成"}
                        //紧接着指定这条订单当前属于哪条节点
                        if (itripHotelOrder.getOrderStatus() == 1) {
                            itripPersonalHotelOrderVO.setOrderProcess(JSONArray.parse(systemConfig.getOrderProcessCancel()));
                            itripPersonalHotelOrderVO.setProcessNode("3");
                        } else if (itripHotelOrder.getOrderStatus() == 0) {
                            itripPersonalHotelOrderVO.setOrderProcess(JSONArray.parse(systemConfig.getOrderProcessOK()));
                            itripPersonalHotelOrderVO.setProcessNode("2");//订单支付
                        } else if (itripHotelOrder.getOrderStatus() == 2) {
                            itripPersonalHotelOrderVO.setOrderProcess(JSONArray.parse(systemConfig.getOrderProcessOK()));
                            itripPersonalHotelOrderVO.setProcessNode("3");//支付成功（未出行）
                        } else if (itripHotelOrder.getOrderStatus() == 3) {
                            itripPersonalHotelOrderVO.setOrderProcess(JSONArray.parse(systemConfig.getOrderProcessOK()));
                            itripPersonalHotelOrderVO.setProcessNode("5");//订单点评
                        } else if (itripHotelOrder.getOrderStatus() == 4) {
                            itripPersonalHotelOrderVO.setOrderProcess(JSONArray.parse(systemConfig.getOrderProcessOK()));
                            itripPersonalHotelOrderVO.setProcessNode("6");//订单完成
                        } else {
                            itripPersonalHotelOrderVO.setOrderProcess(null);
                            itripPersonalHotelOrderVO.setProcessNode(null);
                        }
                        return DtoUtil.returnDataSuccess(itripPersonalHotelOrderVO);
                    } else
                        return DtoUtil.returnFail("没有相关的订单信息", ErrorCode.BIZ_QUERY_IS_NO_ORDERINFO);
                } catch (Exception e) {
                    e.printStackTrace();
                    return DtoUtil.returnFail("查询订单信息失败", ErrorCode.BIZ_QUERY_GET_PERSONAL_ORDERINFO_FAILED);
                }
            } else
                return DtoUtil.returnFail("请输入订单编号", ErrorCode.BIZ_QUERY_PARAM_ORDERID);
        } else {
            return DtoUtil.returnFail("Token不存在", ErrorCode.BIZ_TOKEN_INVALID);
        }

    }


    @ApiOperation(value = "根据订单Id查询房源信息", httpMethod = "GET", produces = "application/json", response = Dto.class, notes = "根据订单ID查看个人订单详情-房型相关信息" +
            "<p>成功：success = ‘true’ | 失败：success = ‘false’ 并返回错误码，如下：</p>" +
            "<p>错误码：</p>" +
            "<p>100529 : 请传递参数：orderId </p>" +
            "<p>100530 : 没有相关订单房型信息 </p>" +
            "<p>100531 : 获取个人订单房型信息错误 </p>" +
            "<p>支持支付类型(roomPayType)：{\"1\":\"在线付\",\"2\":\"线下付\",\"3\":\"不限\"}</p>" +
            "<p>100000 : token失效，请重登录 </p>")
    @RequestMapping(value = "/getpersonalorderroominfo/{orderId}", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Dto getPersonalOrderRoomInfo(@PathVariable @ApiParam(required = true, value = "orderId", name = "订单Id") String orderId, HttpServletRequest request) {
        String token = request.getHeader("token");
        ItripUser currentUser = validationToken.getCurrentUser(token);
        if (EmptyUtils.isNotEmpty(currentUser)) {
            //由于前端接口传参异常，此处先写死订单ID
            orderId = "119";
            ItripPersonalOrderRoomInfoVO itripPersonalOrderRoomInfoVo = itripHotelOrderService.getHotelOrderRoomInfoByOrderId(Long.valueOf(orderId));
            if (itripPersonalOrderRoomInfoVo == null) {
                return DtoUtil.returnFail("没有相关的房型信息", ErrorCode.BIZ_HODEL_ORDER_NO_ROOM_INFO);
            }
            return DtoUtil.returnDataSuccess(itripPersonalOrderRoomInfoVo);
        } else {
            return DtoUtil.returnFail("Token失效", ErrorCode.BIZ_TOKEN_INVALID);
        }
    }


}
